//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Data;
using DotNetNuke.Security;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Search;
using System.Web;
using DotNetNuke.Common.Utilities;
namespace DotNetNuke.Modules.SearchResults
{
	partial class SearchResults : Entities.Modules.PortalModuleBase
	{
		private int _CurrentPage = 1;
		private string _SearchQuery;
		protected int TotalPages = -1;
		protected int TotalRecords;
		protected int CurrentPage {
			get { return _CurrentPage; }
			set { _CurrentPage = value; }
		}
		protected int PageSize {
			get {
				int itemsPage = 10;
				if (!String.IsNullOrEmpty(Convert.ToString(Settings["perpage"])))
				{
					itemsPage = int.Parse(Convert.ToString(Settings["perpage"]));
				}
				return itemsPage;
			}
		}
		private int BindSearchResults(DataTable dt)
		{
			SearchResultsInfoCollection Results = SearchDataStoreProvider.Instance().GetSearchResults(PortalId, _SearchQuery);
			int maxItems = 0;
			if (!String.IsNullOrEmpty(Convert.ToString(Settings["maxresults"])))
			{
				maxItems = int.Parse(Convert.ToString(Settings["maxresults"]));
			} else {
				maxItems = Results.Count;
			}
			if (Results.Count < maxItems || maxItems < 1)
			{
				maxItems = Results.Count;
			}
			int titleLength = 0;
			if (!String.IsNullOrEmpty(Convert.ToString(Settings["titlelength"])))
			{
				titleLength = int.Parse(Convert.ToString(Settings["titlelength"]));
			}
			int descLength = 0;
			if (!String.IsNullOrEmpty(Convert.ToString(Settings["descriptionlength"])))
			{
				descLength = int.Parse(Convert.ToString(Settings["descriptionlength"]));
			}
			int i = 0;
			SearchResultsInfo ResultItem;
			for (i = 0; i <= maxItems - 1; i++) {
				ResultItem = Results[i];
				DataRow dr = dt.NewRow();
				dr["TabId"] = ResultItem.TabId;
				dr["Guid"] = ResultItem.Guid;
				if (titleLength > 0 && titleLength < ResultItem.Title.Length) {
					dr["Title"] = ResultItem.Title.Substring(0, titleLength);
				} else {
					dr["Title"] = ResultItem.Title;
				}
				dr["Relevance"] = ResultItem.Relevance;
				if (descLength > 0 && descLength < ResultItem.Description.Length) {
					dr["Description"] = ResultItem.Description.Substring(0, descLength);
				} else {
					dr["Description"] = ResultItem.Description;
				}
				dr["PubDate"] = ResultItem.PubDate;
				dt.Rows.Add(dr);
			}
			return Results.Count;
		}

		private void BindData()
		{
			using (DataTable dt = new DataTable())
			{
				DataColumn dc = new DataColumn("TabId");
				dt.Columns.Add(new DataColumn("TabId", typeof(System.Int32)));
				dt.Columns.Add(new DataColumn("Guid", typeof(System.String)));
				dt.Columns.Add(new DataColumn("Title", typeof(System.String)));
				dt.Columns.Add(new DataColumn("Relevance", typeof(System.Int32)));
				dt.Columns.Add(new DataColumn("Description", typeof(System.String)));
				dt.Columns.Add(new DataColumn("PubDate", typeof(System.DateTime)));

				int count = Null.NullInteger;

				if (_SearchQuery.Length > 0)
				{
					count = BindSearchResults(dt);
				}
				DataView dv = new DataView(dt);
				dv.Sort = "Relevance DESC";
				dgResults.PageSize = PageSize;
				dgResults.DataSource = dv;
				dgResults.DataBind();
				if (count == 0)
				{
					dgResults.Visible = false;
					lblMessage.Text = string.Format(Localization.GetString("NoResults", LocalResourceFile), _SearchQuery);
				}
				else
				{
					lblMessage.Text = string.Format(Localization.GetString("Results", LocalResourceFile), _SearchQuery);
				}
				if (count <= dgResults.PageSize)
				{
					ctlPagingControl.Visible = false;
				}
				else
				{
					ctlPagingControl.Visible = true;
				}
				ctlPagingControl.TotalRecords = count;
			}
			
			ctlPagingControl.PageSize = dgResults.PageSize;
			ctlPagingControl.CurrentPage = CurrentPage;
			
		}
		protected string FormatDate(System.DateTime pubDate)
		{
			return pubDate.ToString();
		}
		protected string FormatRelevance(int relevance)
		{
			string relevanceString = Null.NullString;
			if (relevance > 0)
			{
				relevanceString = Services.Localization.Localization.GetString("Relevance", this.LocalResourceFile) + relevance.ToString();
			}
			return relevanceString;
		}
		protected string FormatURL(int TabID, string Link)
		{
			string strURL;
			if (String.IsNullOrEmpty(Link)) {
				strURL = Common.Globals.NavigateURL(TabID);
			} else {
				strURL = Common.Globals.NavigateURL(TabID, "", Link);
			}
			return strURL;
		}
		protected string ShowDescription()
		{
			string strShow;
			if (!String.IsNullOrEmpty(Convert.ToString(Settings["showdescription"])))
			{
				if (Convert.ToString(Settings["showdescription"]) == "Y")
				{
					strShow = "True";
				} else
				{
					strShow = "False";
				}
			}
			else 
			{
				strShow = "True";
			}
			return strShow;
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			dgResults.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(dgResults_PageIndexChanged);
			ctlPagingControl.PageChanged += new EventHandler(ctlPagingControl_PageChanged);

			PortalSecurity objSecurity = new PortalSecurity();
			if (Request.Params["Search"] != null)
			{
				_SearchQuery =  HttpContext.Current.Server.HtmlEncode(objSecurity.InputFilter(Request.Params["Search"].ToString(), PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoMarkup));
			}
			if (!String.IsNullOrEmpty(_SearchQuery))
			{
				if (!Page.IsPostBack)
				{
					BindData();
				}
			}
			else
			{
				if( this.IsEditable)
				{
                    Skin.AddModuleMessage(this, Localization.GetString("ModuleHidden", this.LocalResourceFile), UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
                }
				else
				{
					this.ContainerControl.Visible = false;
                }
			}
		}
		private void dgResults_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dgResults.CurrentPageIndex = e.NewPageIndex;
			BindData();
		}
		protected void ctlPagingControl_PageChanged(object sender, System.EventArgs e)
		{
			CurrentPage = ctlPagingControl.CurrentPage;
			dgResults.CurrentPageIndex = CurrentPage - 1;
			BindData();
		}
	}
}
